package demo6;

import java.util.Objects;

class Person{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public boolean equals(Object obj) {
//        Person person=(Person) obj;
//        if(this.name.equals(person.name)){
//            return true;
//        }else return false;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class Text2 {
    public static void main(String[] args) {
        Person person1=new Person("zhangsan",10);
        Person person2=new Person("zhangsan",10);
        System.out.println(person1.equals(person2));//equals为父类object的方法
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());

    }
}
//自定义类型，一定要重写hashcode，equals
//可选择的方法：get set toString